/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Object;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author PhongLK60473
 */

public class CustomerOrder implements Serializable{
    private ArrayList orderBook ;
    private int Id;
    private int Userid;
    private int StatusId;
    private String Status;
    private float TotalPrice;
    private Date date;
    private Date time;
    private User userObj;
    //private Date
    private String Description;
    public static Hashtable HStatus;
    public String getDescription() {
        return Description;
    }

    public void setDescription(String Description) {
        this.Description = Description;
    }

    public int getId() {
        return Id;
    }

    public void setId(int Id) {
        this.Id = Id;
    }

    public User getUserObj() {
        return userObj;
    }

    public void setUserObj(User userObj) {
        this.userObj = userObj;
    }

    public String getStatus() {
        return Status;
    }

    public void setStatus(String Status) {
        this.Status = Status;
    }

    public int getStatusId() {
        return StatusId;
    }

    public void setStatusId(int StatusId) {
        this.StatusId = StatusId;
    }

    public float getTotalPrice() {
        return TotalPrice;
    }

    public void setTotalPrice(float TotalPrice) {
        this.TotalPrice = TotalPrice;
    }

    public int getUserid() {
        return Userid;
    }

    public void setUserid(int Userid) {
        this.Userid = Userid;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    

    public ArrayList getOrderBook() {
        return orderBook;
    }

    public void setOrderBook(ArrayList orderBook) {
        this.orderBook = orderBook;
    }

    public CustomerOrder(int Id, int Userid, int StatusId, String Status, float TotalPrice, Date date, Date time, String Description) {
        this.Id = Id;
        this.Userid = Userid;
        this.StatusId = StatusId;
        this.Status = Status;
        this.TotalPrice = TotalPrice;
        this.date = date;
        this.time = time;
        this.Description = Description;
    }

    public static boolean changeStatus(HttpServletRequest request){
        boolean rt=false;
        int orderid = 0 ;
        int statusid = 0;
        try {
            orderid = Integer.parseInt(request.getParameter("orderid"));
            statusid = Integer.parseInt(request.getParameter("statusid"));
        } catch (Exception e){
            rt=false;
        }
        if(changeStatus(orderid, statusid)){
            rt=true;
        }else{
            rt=false;
        }
        if(rt){
            Page.setMessage("Status has change", request);
        }else{
            Page.setMessage("Status can not change", request);
        }
        return rt;
    }
    public static boolean changeStatus(int orderId,int statusId){
        boolean rt=false;
        Database db = new Database();
        if(db.isOK){
            String strSQL;
            PreparedStatement stm=null;
            int rs;
            try {
                strSQL = "UPDATE Orders SET statusid = ? "
                        + "WHERE id = ?";
                stm = db.con.prepareStatement(strSQL);
                stm.setInt(1, statusId);
                stm.setInt(2, orderId);

                rs = stm.executeUpdate();

                if(rs>0){
                    rt=true;
                }else{
                    rt=false;
                }
            } catch (SQLException e) {
                rt=false;
            } finally {
                try{
                    stm.close();
                    db.con.close();
                } catch (SQLException e){

                }
            }
        }else{
            rt=false;
        }
        return rt;
    }
    public static CustomerOrder[] getAllOrders(int userId) {
        Database db = new Database();
        ArrayList alOrders = new ArrayList();
        boolean getUserInfo=false;
        if(db.isOK){
            String sql;
            PreparedStatement stm=null;
            ResultSet rs=null;
            try {
                sql = "SELECT * FROM OrderStatus ORDER BY id ASC";
                stm = db.con.prepareStatement(sql);
                rs=stm.executeQuery();
                Hashtable Status = new Hashtable();
                while(rs.next()){
                    Status.put(rs.getInt(1)+"", rs.getString(2));
                }
                CustomerOrder.HStatus = Status;
                rs=null;
                if(userId<1){
                    sql = "SELECT * FROM Orders "
                            + "ORDER BY statusid ASC,date DESC, time DESC";
                    stm = db.con.prepareStatement(sql);
                    getUserInfo=true;
                }else{
                    sql = "SELECT * FROM Orders WHERE userid = ? "
                            + " ORDER BY statusid ASC,date DESC, time DESC";
                    stm = db.con.prepareStatement(sql);
                    stm.setInt(1, userId);
                }
                rs=stm.executeQuery();
                while(rs.next()){
                    int statusid = rs.getInt(3);
                    CustomerOrder newOrder = new CustomerOrder(rs.getInt(1),
                            rs.getInt(2),statusid,
                            (String) Status.get(statusid+""),rs.getFloat(4),
                            rs.getDate(5),rs.getTime(6),rs.getString(7));
                    if(getUserInfo){
                        newOrder.userObj = new User(newOrder.Userid);
                    }
                    alOrders.add(newOrder);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    rs.close();
                    stm.close();
                    db.con.close();
                } catch (SQLException e2){
                    e2.printStackTrace();
                }
            }
        } else {

        }


        CustomerOrder[] custOrders = new CustomerOrder[alOrders.size()];
        alOrders.toArray(custOrders);
        return custOrders;
    }

    
}
